/*
 * Copyrights of MYeBills. Do not copy or distribute without permissions.
 *
 * TODO 
 */
package com.mbbmap.app.dao;

/**
 * @author Lue
 *
 * JSP result page pagination navigation
 */
public class Pagination {
    private int totalRecord=0;
    private int totalPages = 0;
    private int maxRecordsPerPage =0;
    private int pageNo=1;
    private int pageRecordStartNo=0;
    private int pageRecordEndNo=0;
    
    public void setTotalRecord (int total) {
        if (totalRecord < 0) {
            totalRecord = 0;
        } else {
            // count the no. of pages again when total records was set
            totalRecord = total;
            resetTotalPages ();
        }
            
    }
    
    public void setPageMaxRecord (int pageMax) {
        if (pageMax < 1)
            maxRecordsPerPage = 1;
        else
            maxRecordsPerPage = pageMax;
    }
    
    // PageNum start from 1
    public void setPageNo (int pageNum) {
        if (pageNum < 1)
            pageNo = 1;
        else
            pageNo = pageNum;
    }
    
    public int getTotalRecord () {
        return totalRecord;
    }
    
    public int getPageMaxRecord () {
        return maxRecordsPerPage;
    }
    
    public int getTotalPages() {
        
        if (totalPages > 0) {
            return totalPages;
        
        } else {
    		return 0;
        }
        
    }
    
    public int getCurrentPageNo () {
        return pageNo;
    }
    
    public void resetTotalPages() {
        totalPages = totalRecord / maxRecordsPerPage;
        
		if ((totalRecord % maxRecordsPerPage) > 0){
			 totalPages++;
		}
        
    }
    
    // records no start from 1
    public int getPageRecordStartNo () {
        return ( (pageNo-1) * maxRecordsPerPage) + 1;
    }
   
    public int getPageRecordEndNo () {
        return ( pageNo * maxRecordsPerPage) ;
    }
    
    public void nextPage() {
        if ( (pageNo + 1) <= totalPages ) {
            pageNo ++;
        }
        else {
            pageNo = 1;
        }
    }
    
    public void previousPage() {
        if ( (pageNo -1 ) > 0 ) {
            pageNo --;
        }
        else {
            pageNo = totalPages;
        }
    }

    public void firstPage () {
        pageNo = 1;
    }
    
    public void lastPage () {
        pageNo = totalPages;
    }

    
    public boolean isNextPage () {
        if ( (pageNo + 1) <= totalPages )
            return true;
        else 
            return false;
    }

    public boolean isPreviousPage () {
        if ( (pageNo -1 ) > 0 )
            return true;
        else 
            return false;
    }

    public boolean isFirstPage () {
        if ( pageNo == 1 )
            return false;
        else 
            return true;
    }

    public boolean isLastPage () {
        if ( pageNo >= totalPages )
            return false;
        else 
            return true;
    }
    
    
    

}
